/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import binding.comments.Comments;
import binding.orderdetails.OrderDetails;
import binding.orders.Orders;
import binding.products.Products;
import binding.userdetails.UserDetails;
import binding.users.Users;
import core.DBEntities;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.datatype.DatatypeConfigurationException;
import utils.XmlUtils;

public class XmlController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, DatatypeConfigurationException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            String currentPage = request.getParameter("current");
            
            if (action.equals("FindResource")) {
                String type = request.getParameter("type");
                String name = request.getParameter("name");
                String webPath = getServletContext().getRealPath("/");
                String folderMap = "";
                if (type.equals("xml")) {
                    folderMap = webPath + "WEB-INF/XmlDoc/";
                } else if (type.equals("xsl")) {
                    folderMap = webPath + "WEB-INF/XslDoc/";
                } 
                String filePath = folderMap + name;
                String fileContent = XmlUtils.transformToString(filePath);
                response.setContentType("text/xml");
                out.write(fileContent);
            } else if (action.equals("UserDetailsXmlCheck")) {
                UserDetails userDetails = DBEntities.getListUserDetail();
                if (userDetails != null) {
                    String webPath = getServletContext().getRealPath("/");
                    String filePath = webPath + "WEB-INF/XmlDoc/UserDetails.xml";
                    XmlUtils.marshalXml(userDetails, filePath);
                    request.getRequestDispatcher(currentPage).forward(request, response);
                }
            } else if (action.equals("UsersXmlCheck")) {
                Users users = DBEntities.getListUser();
                if (users != null) {
                    String webPath = getServletContext().getRealPath("/");
                    String filePath = webPath + "WEB-INF/XmlDoc/Users.xml";
                    XmlUtils.marshalXml(users, filePath);
                    request.getRequestDispatcher(currentPage).forward(request, response);
                }

            } else if (action.equals("OrdersXmlCheck")) {
                Orders orders = DBEntities.getOrders();
                if (orders != null) {
                    String webPath = getServletContext().getRealPath("/");
                    String filePath = webPath + "WEB-INF/XmlDoc/Orders.xml";
                    XmlUtils.marshalXml(orders, filePath);
                    request.getRequestDispatcher(currentPage).forward(request, response);
                }
            } else if (action.equals("OrderDetailsXmlCheck")) {
                OrderDetails orders = DBEntities.getOrderDetails();
                if (orders != null) {
                    String webPath = getServletContext().getRealPath("/");
                    String filePath = webPath + "WEB-INF/XmlDoc/OrderDetails.xml";
                    XmlUtils.marshalXml(orders, filePath);
                    request.getRequestDispatcher(currentPage).forward(request, response);
                }
            } else if (action.equals("ProductsXmlCheck")) {
                Products resList = DBEntities.getProducts();
                if (resList != null) {
                    String webPath = getServletContext().getRealPath("/");
                    String filePath = webPath + "WEB-INF/XmlDoc/Products.xml";
                    XmlUtils.marshalXml(resList, filePath);
                    request.getRequestDispatcher(currentPage).forward(request, response);
                }
            } else if (action.equals("CommentXmlCheck")) {
                Comments comList = DBEntities.getListComments();
                if (comList != null) {
                    String webPath = getServletContext().getRealPath("/");
                    String filePath = webPath + "WEB-INF/XmlDoc/Comments.xml";
                    XmlUtils.marshalXml(comList, filePath);
                    request.getRequestDispatcher(currentPage).forward(request, response);
                }
            }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (DatatypeConfigurationException ex) {
            Logger.getLogger(XmlController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(XmlController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (DatatypeConfigurationException ex) {
            Logger.getLogger(XmlController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(XmlController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
